/**
 * @Title: GoogleKaptcha
 * @Package cn.com.artemis.framework.service.captcha.utils
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2016/11/7 17:37
 * @version V1.0
 */
package cn.com.yiqi.wsc.service.captcha.utils;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

/**
 *
 *@ClassName: GoogleKaptcha
 * @Description: TODO
 * @author 会员系统组-胥钰淋
 * @date 2016/11/7 17:37
 *
 */
@Service
public class GoogleKaptcha {

    private static final int LENGTH = 4;

    private Properties props = new Properties();
    private Producer kaptchaProducer = null;
    private String sessionKeyValue = null;
    private String sessionKeyDateValue = null;

    public GoogleKaptcha () {
        this.props.put("kaptcha.border", "no");
        this.props.put("kaptcha.border.color", "105,179,90");
        this.props.put("kaptcha.textproducer.font.color", "red");
        this.props.put("kaptcha.image.width", "136");
        this.props.put("kaptcha.image.height", "32");
        this.props.put("kaptcha.textproducer.font.size", "30");
        this.props.put("kaptcha.session.key", "KAPTCHA_SESSION_KEY");
        this.props.put("kaptcha.textproducer.char.length", LENGTH);
        this.props.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config = new Config(this.props);
        this.kaptchaProducer = config.getProducerImpl();
        this.sessionKeyValue = config.getSessionKey();
        this.sessionKeyDateValue = config.getSessionDate();
    }

    /**
     * 获取验证码
     * @param request
     * @param response
     */
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) {
        response.setDateHeader("Expires", 0L);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = kaptchaProducer.createText();
        request.getSession().setAttribute(sessionKeyValue, capText);
        request.getSession().setAttribute(sessionKeyDateValue, new Date());
        BufferedImage bi = kaptchaProducer.createImage(capText);

        try {
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
        } catch (IOException var5) {
            var5.printStackTrace();
        }
    }

    /**
     * 验证验证码
     * @param request
     * @param captcha
     * @return
     */
    public boolean validate(HttpServletRequest request, String captcha) {
        if(StringUtils.isEmpty(captcha)) {
            return false;
        } else {
            try {
                String e = (String)request.getSession().getAttribute("KAPTCHA_SESSION_KEY");
                boolean var4 = captcha.equals(e.toUpperCase());
                return var4;
            } catch (Exception var7) {
                var7.printStackTrace();
            } finally {
                request.getSession().removeAttribute("KAPTCHA_SESSION_KEY");
            }
            return false;
        }
    }

}
